<?php
/**
 * Class that operate on table 'doctors_category'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class DoctorsCategoryMySqlDAO implements DoctorsCategoryDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return DoctorsCategoryMySql 
	 */
	public function load($doctorsCategory){
		$sql = 'SELECT * FROM doctors_category WHERE category_id = "'.$doctorsCategory->category_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */

	
	public function doctorcategory_list($doctorsCategory){
		$sql = 'SELECT category_id,category_name FROM doctors_category';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllSearchBy($doctorsCategory){
		$sql = 'SELECT * FROM doctors_category WHERE '.$doctorsCategory->search_by.' like"%'.$doctorsCategory->search_by_value.'%" order by '.$doctorsCategory->order_by.' '.$doctorsCategory->sort_order.' limit '.$doctorsCategory->results_per_page.' offset '.$doctorsCategory->offset.'';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	 public function SearchByCount($doctorsCategory){
		$sql = 'SELECT count(*) FROM doctors_category WHERE '.$doctorsCategory->search_by.' like"%'.$doctorsCategory->search_by_value.'%" order by '.$doctorsCategory->order_by.' '.$doctorsCategory->sort_order.' limit '.$doctorsCategory->results_per_page.' offset '.$doctorsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	public function queryAll($doctorsCategory){
		$sql = 'SELECT * FROM doctors_category order by '.$doctorsCategory->order_by.' '.$doctorsCategory->sort_order.' limit '.$doctorsCategory->results_per_page.' offset '.$doctorsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function queryAllCount($doctorsCategory){
		$sql = 'SELECT count(*) FROM doctors_category order by '.$doctorsCategory->order_by.' '.$doctorsCategory->sort_order.' limit '.$doctorsCategory->results_per_page.' offset '.$doctorsCategory->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}  
	
	/**
 	 * Delete record from table
 	 * @param doctorsCategory primary key
 	 */
	public function delete($doctorsCategory){
		$sql = 'UPDATE doctors_category SET status = "'.$doctorsCategory->status.'" WHERE category_id = "'.$doctorsCategory->category_id.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param DoctorsCategoryMySql doctorsCategory
 	 */
	public function insert($doctorsCategory){
		$sql = 'INSERT INTO doctors_category (category_name, admin_id, created_date, updated_date) VALUES ("'.$doctorsCategory->category_name.'", "'.$doctorsCategory->admin_id.'", "'.$doctorsCategory->created_date.'", "'.$doctorsCategory->updated_date.'")';
		$sqlQuery = new SqlQuery($sql);
		$id = $this->executeInsert($sqlQuery);	
		$doctorsCategory->category_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param DoctorsCategoryMySql doctorsCategory
 	 */
	public function update($doctorsCategory){
		$sql = 'UPDATE doctors_category SET category_name = ?, admin_id = ?, status = ?, updated_date = ? WHERE category_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($doctorsCategory->category_name);
		$sqlQuery->set($doctorsCategory->admin_id);
		$sqlQuery->setNumber($doctorsCategory->status);
		$sqlQuery->set($doctorsCategory->updated_date);
		$sqlQuery->setNumber($doctorsCategory->category_id);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM doctors_category';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByCategoryName($value){
		$sql = 'SELECT * FROM doctors_category WHERE category_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAdminId($value){
		$sql = 'SELECT * FROM doctors_category WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM doctors_category WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM doctors_category WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM doctors_category WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByCategoryName($value){
		$sql = 'DELETE FROM doctors_category WHERE category_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAdminId($value){
		$sql = 'DELETE FROM doctors_category WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM doctors_category WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM doctors_category WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM doctors_category WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return DoctorsCategoryMySql 
	 */
	protected function readRow($row){
		$doctorsCategory = new DoctorsCategory();
		
		$doctorsCategory->category_id = $row['category_id'];
		$doctorsCategory->category_name = $row['category_name'];
		$doctorsCategory->admin_id = $row['admin_id'];
		$doctorsCategory->status = $row['status'];
		$doctorsCategory->created_date = $row['created_date'];
		$doctorsCategory->updated_date = $row['updated_date'];

		return $doctorsCategory;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return DoctorsCategoryMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>